/**
 * @name: 请求层
 * @author: kahu
 * @date: 2023-10-16 18:03
 * @description：request
 * @update: 2023-10-16 18:03
 * */
const baseUrl = '';
const promiseFetch = (url: string, options?: any): Promise<any> => {
  const requestURL = `${baseUrl}${url}`;
  return new Promise((resolve, reject) => {
    useFetch(requestURL, { ...options })
      .then(({ data, error }): any => {
        if (error.value) {
          reject(error.value);
          return;
        }
        const value = data.value;
        if (!value) {
          // 这里处理错误回调
          reject(value);
          // $router.replace('/reject/' + value.status)
        } else {
          resolve(ref(value));
        }
      })
      .catch((err: Error) => {
        reject(err);
      });
  });
};

class Http {
  get<T>(url: string, params?: any): Promise<T> {
    return promiseFetch(url, { method: 'get', params });
  }

  post<T>(url: string, params?: any): Promise<T> {
    return promiseFetch(url, { method: 'post', params });
  }

  put<T>(url: string, body?: any): Promise<T> {
    return promiseFetch(url, { method: 'put', body });
  }

  delete<T>(url: string, body?: any): Promise<T> {
    return promiseFetch(url, { method: 'delete', body });
  }
}
export default new Http();
